const Sequelize = require('sequelize')
const sequelize = require('../db/dbconfig')
const url = require('url')
const fs = require('querystring')
const ProjectCommentModel = require('../db/models/project_comments')
const ProjectInfoModel = require('../db/models/project_infos')
const UserinfoModel = require('../db/models/users')
const projectCommentModel = ProjectCommentModel(sequelize)
const projectInfoModel = ProjectInfoModel(sequelize)
const userinfoModel = UserinfoModel(sequelize)
const Op = Sequelize.Op

// 增
exports.addProjectComment = async (req, res) => {
 try {
   const projectComment = req.body
   const user = req.auth
   let constLevel = await projectCommentModel.findAndCountAll({
     attributes: [sequelize.fn('COUNT', sequelize.col('com_id'))],
     where: {
       is_delete: 0,
       pro_id: projectComment.pro_id
     },
     raw: true
   })
   let results = await projectCommentModel.create({
     user_id: user.user_id,
     pro_id: projectComment.pro_id,
     com_content: projectComment.com_content,
     com_level: constLevel.count+1
   })
   res.myMsg(results,0)
 } catch (e) {
   res.myMsg(e)
 }
}

// 删
exports.delProjectComment = async (req, res) => {
  try {
    let results = await projectCommentModel.update({
      is_delete: 1
    },{
      where: {
        com_id: req.query.id
      }
    })
    if(results[0] === 0) return res.myMsg('项目评论不存在')
    res.myMsg('项目评论删除成功',200)
  } catch (e) {
    res.myMsg(e)
  }
}

// 改
exports.updateProjectComment = async (req, res) => {
  try {
    const projectComment = req.body
    let is_exist = await projectCommentModel.findOne({
      where: {
        is_delete: 0,
        com_id: projectComment.com_id
      }
    })
    if(is_exist === null) return res.myMsg('修改失败,评论不存在')
    let results = await projectCommentModel.update({
      com_content: projectComment.com_content
    },{
      where: {
        com_id: projectComment.com_id
      }
    })
    res.myMsg('项目评论修改成功')
  } catch (e) {
    res.myMsg(e)
  }
}

// 查
exports.getProjectCommentAll = async (req, res) => {
  try{
    // const {query} = url.parse(req.url, false);
    // let temp = parseInt(fs.parse(query).offset[0])
    // const offset = temp%2===0? temp:temp-1
    let offset = 0
    // 关联外键
    projectCommentModel.belongsTo(projectInfoModel,{
      // as: 'cate',
      foreignKey: 'pro_id',
      targetKey: 'pro_id'
    })
    projectCommentModel.belongsTo(userinfoModel,{
      // as: 'cate',
      foreignKey: 'user_id',
      targetKey: 'user_id'
    })
    let results = await projectCommentModel.findAll({
      include: [
        {
          attributes: ['pro_title'],
          // as: 'cate',
          model: projectInfoModel,
        },{
          attributes: ['nickname'],
          // as: 'cate',
          model: userinfoModel,
        }
      ],
      where: {
        is_delete: 0
      },
      order: [
          ['com_id', 'asc']
      ],
      limit: 10,
      offset
    })
    res.type('application/json');
    res.myMsg(results,200)
  }catch (e) {
    res.myMsg(e)
  }
}
